<template>
    <div class="baseStyle">
        <div class="tableDataQuiz">
            <el-row class="heard">
                <el-col>
                    <span>实验数据</span>
                </el-col>
            </el-row>
            <el-row v-show="quizType === '001'" v-for="(item, index) in tableList" :key="index">
                <el-col>
                    <el-row><span>{{ getTableThreshouldType(item.type) }}</span></el-row>
                    <el-row><span>{{ getTableTitle(item.type) }}</span></el-row>
                    <el-row>
                        <el-col :span="4">
                            <!-- 第一列 -->
                            <el-row v-for="(column, columnIndex) in getColumn(item.type)" :key="columnIndex">
                                <el-col><span>{{ column }}</span></el-col>
                            </el-row>
                            <!-- 剩余 -->
                        </el-col>
                        <el-col :span="2" v-for="(threshold, thresholdIndex) in item.thresholdList" :key="thresholdIndex">
                           <div v-if="threshold.type === '001'">
                               <el-row><el-input disabled v-model="threshold.u1" /></el-row>
                               <el-row><el-input v-model="threshold.i2" @change="getThresholdG(threshold)"/></el-row>
                               <el-row><el-input disabled v-model="threshold.g" /></el-row>
                           </div>
                           <div v-if="threshold.type === '002' || threshold.type === '008'">
                               <el-row><el-input disabled v-model="threshold.r"/></el-row>
                               <el-row><el-input v-model="threshold.i2" @change="getThresholdG(threshold)"/></el-row>
                           </div>
                           <div v-if="threshold.type === '003'">
                               <el-row><el-input disabled v-model="threshold.i"/></el-row>
                               <el-row><el-input v-model="threshold.u2" @change="getThresholdG(threshold)" /></el-row>
                               <el-row><el-input disabled v-model="threshold.r1"/></el-row>
                           </div>
                           <div v-if="threshold.type === '004' || threshold.type === '006'">
                               <el-row><el-input disabled v-model="threshold.r" @change="getThresholdG(threshold)"/></el-row>
                               <el-row><el-input v-model="threshold.u2"/></el-row>
                           </div>
                           <div v-if="threshold.type === '005'">
                               <el-row><el-input disabled v-model="threshold.u1" /></el-row>
                               <el-row><el-input v-model="threshold.u2" @change="getThresholdG(threshold)"/></el-row>
                               <el-row><el-input disabled v-model="threshold.u"/></el-row>
                           </div>
                           <div v-if="threshold.type === '007'">
                               <el-row><el-input disabled v-model="threshold.i"/></el-row>
                               <el-row><el-input v-model="threshold.u2" @change="getThresholdG(threshold)"/></el-row>
                               <el-row><el-input disabled v-model="threshold.b"/></el-row>
                           </div>
                        </el-col>
                    </el-row>
                </el-col>
            </el-row>
            <el-row v-if="quizType === '002'">
                <el-col>
                    <el-row><span>{{ getTableTitle('009') }}</span></el-row>
                    <el-row>
                        <el-col :span="4" style="margin-top: 50px">
                            <el-row style="margin-top: 30px" v-for="(column, columnIndex) in getColumn('009')" :key="columnIndex">
                                <el-col><span>{{ column }}</span></el-col>
                            </el-row>
                        </el-col>
                        <el-col :span="20">
                            <el-table :data="thresholdTableList"  border stripe>
                                <el-table-column prop="uac" label="UAC（V）" width="300">
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.uac"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="uce" label="UCE（V）" width="300">
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.uce"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="ubd" label="UBD（V）" width="300">
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.ubd"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="udf" label="UDF（V）" width="300">
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.udf"></el-input>
                                    </template>
                                </el-table-column>
                                <el-table-column prop="ucd" label="UCD（V）" width="300">
                                    <template slot-scope="scope">
                                        <el-input v-model="scope.row.ucd"></el-input>
                                    </template>
                                </el-table-column>
                            </el-table>
                        </el-col>
                    </el-row>
                </el-col>
            </el-row>
            <el-row class="heard">
                <el-col><span>实验结论</span></el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-input type="textArea" v-model="thresholdForm.conclusion"></el-input>
                </el-col>
            </el-row>
            <el-row style="margin-top:20px">
                <el-col>
                    <el-button type="primary" size="small" @click="saveReport">提交报告</el-button>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            tableList: [],
            quizType: '',
            conclusion: '',
            thresholdForm: {
                quizId: '',
                userQuizId:'',
                conclusion: '',
                userThresholdSaveCustomParamList: []
            },
            userThresholdSaveCustomParam:{
                type: '',
                userThresholdList: []
            },
            thresholdTableList: [{
                uac: null,
                uce: null,
                ubd: null,
                udf: null,
                ucd: null
            },{
                uac: null,
                uce: null,
                ubd: null,
                udf: null,
                ucd: null
            },{
                uac: null,
                uce: null,
                ubd: null,
                udf: null,
                ucd: null
            },{
                uac: null,
                uce: null,
                ubd: null,
                udf: null,
                ucd: null
            },{
                uac: null,
                uce: null,
                ubd: null,
                udf: null,
                ucd: null
            },{
                uac: null,
                uce: null,
                ubd: null,
                udf: null,
                ucd: null
            },]
        }
    },  
    mounted() {
        this.getTableList()
        this.quizType = this.$route.query.quizType
    },
    methods: {
        getTableThreshouldType(type) {
            if(type === '001') {
                return '1.	VCCS'
            }
            if(type === '003') {
                return '2.	CCVS'
            }
            if(type === '005') {
                return '3.	VCVS'
            }
            if(type === '007') {
                return '4.	CCCS'
            }
            if(type === '009') {
                return '填入数据'
            }
        },
        getTableTitle(type) {
            if(type === '001') {
                return '表1-1-1 RL=0时的测量'
            }
            if(type === '002') {
                return '表1-1-2 保持U1=3.00V的测量'
            }
            if(type === '003') {
                return '表1-2-1 RL=∞时的测量'
            }
            if(type === '004') {
                return '表1-2-2 保持I1=2.00mA的测量'
            }
            if(type === '005') {
                return '表1-3-1 RL=∞时的测量'
            }
            if(type === '006') {
                return '表1-3-2 保持U1=3.00V的测量'
            }
            if(type === '007') {
                return '表1-4-1 RL=0时的测量'
            }
            if(type === '008') {
                return '表1-4-2 保持I1=2.00mA的测量'
            }
            if(type === '009') {
                return '表3-1'
            }

        },
        getTableList() {
            let url = '/threshold/find'
            this.$http.post(url,{}).then(data => {
                this.tableList = data.data.data
            }).catch(() => {
            })
        },
        getColumn(type) {
            if(type === '001') {
                return ['U1(V)','I2=(mA)','g = I_2/U_1  (s)']
            }
            if(type === '002') {
                return ['RL(Ω)','I2=(mA)']
            }
            if(type === '003') {
                return ['I1(mA)','U2(V)','r = U_2/I_1  (Ω)']
            }
            if(type === '004') {
                return ['RL(Ω)','U2(V)']
            }
            if(type === '005') {
                return ['U1(V)','U2(V)','u = U_2/U_1 ']
            }
            if(type === '006') {
                return ['RL(Ω)','U2(V)']
            }
            if(type === '007') {
                return ['I1(mA)','U2(V)','β=I_2/I_1 (绝对值) ']
            }
            if(type === '008') {
                return ['RL(Ω)','I2=(mA)']
            }
            if(type === '009') {
                return ['US1单独作用','US2单独作用','IS单独作用','US1、US2、IS共同作用','共同作用时的理论计算值','误差']
            }
        },
        saveReport() {
            this.thresholdForm.quizId = this.$route.query.id
            this.thresholdForm.userQuizId = this.$route.query.userQuizId 
            this.thresholdForm.quizType = this.quizType
            this.thresholdForm.userThresholdSaveCustomParamList = []
            if(this.quizType === '001') {
                if(!this.arrayIsEmpty(this.tableList)) {
                    this.tableList.forEach(v => {
                        var userThresholdSaveCustomParam = {
                            type: '',
                            userThresholdList: []
                        }
                        if(v.type === '003') {
                            v.thresholdList.forEach(t => {
                                this.$set(t,'i1',t.i)
                            }) 
                        }
                        if(v.type === '006') {
                            v.thresholdList.forEach(t => {
                                this.$set(t,'r1',t.r)
                            }) 
                        }
                        if(v.type === '007') {
                            v.thresholdList.forEach(t => {
                                this.$set(t,'i1',t.i)
                            }) 
                        }
                        if(v.type === '008') {
                            v.thresholdList.forEach(t => {
                                this.$set(t,'r1',t.r)
                            }) 
                        }
                        userThresholdSaveCustomParam.type = v.type
                        userThresholdSaveCustomParam.userThresholdList = JSON.parse(JSON.stringify(v.thresholdList))
                        this.thresholdForm.userThresholdSaveCustomParamList.push(userThresholdSaveCustomParam)
                    })
                    let url = '/threshold/save'
                    this.$http.post(url,this.thresholdForm).then(data => {
                        if (data.data.success === true) {
                            this.$router.push({path:'/quiz'})
                        } else {
                            this.$message.error(data.message)
                        }
                    })
                }
            }
            if(this.quizType === '002') {
                this.thresholdTableList.forEach(v => {
                    this.$set(v,'type','009')
                })
                var userThresholdSaveCustomParam = {
                    type: '009',
                    userThresholdList: JSON.parse(JSON.stringify(this.thresholdTableList))
                }
                this.thresholdForm.userThresholdSaveCustomParamList.push(userThresholdSaveCustomParam)
                let url = '/threshold/save'
                this.$http.post(url,this.thresholdForm).then(data => {
                    if (data.data.success === true) {
                        this.$router.push({path:'/quiz'})
                    } else {
                        this.$message.error(data.message)
                    }
                })
            }
        },
        // 判断数组是否为空
        arrayIsEmpty(array) {
        if(array === undefined || array === null || array.length < 1) {
            return true
        }
        return false
        },
        getThresholdG(threshold) {
            if(threshold.type === '001') {
                this.$set(threshold,'i2', Number(threshold.i2) * -1)
                var i2 = Number(threshold.i2)
                var u1 = Number(threshold.u1)
                this.$set(threshold,'g', Number(i2/u1).toFixed(5))
            }
            if(threshold.type === '002' || threshold.type === '008') {
                this.$set(threshold,'i2', Number(threshold.i2) * -1)
            }
            if(threshold.type === '003') {
                var u2 = Number(threshold.u2)
                var i = Number(threshold.i)
                this.$set(threshold,'r1', Number(u2/i).toFixed(5))
            }
            if(threshold.type === '005') {
                var u2 = Number(threshold.u2)
                var u1 = Number(threshold.u1)
                this.$set(threshold,'u', Number(u2/u1).toFixed(5))
            }
            if(threshold.type === '007') {
                this.$set(threshold,'u2', Number(threshold.u2) * -1)
                var u2 = Number(threshold.u2)
                var i = Number(threshold.i)
                this.$set(threshold,'b', Number(Number(u2/i) * -1).toFixed(5))
            }

        }
    }
}
</script>

<style>
.tableDataQuiz .heard{
    margin-top: 20px;
    font-size: 25px;
    font-weight: 600;
}
</style>

